<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()" label-position="left" label-width="130px" size="mini">
      <el-row>
        <el-col :span="12">
          <el-form-item label="工夹具名称">
            <el-input v-model="dataForm.name" placeholder="参数名" clearable style="width: 500px"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">

          <el-form-item label="工夹具名称">
            <el-input v-model="dataForm.name1" placeholder="参数名" clearable style="width: 500px"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>

        <el-col :span="12">
          <el-form-item label="工作部门">
            <el-input v-model="dataForm.workcell" placeholder="参数名" clearable style="width: 500px"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">

          <el-form-item label="类别">
            <el-input v-model="dataForm.family" placeholder="参数名" clearable style="width: 500px"></el-input>
          </el-form-item>
        </el-col>

      </el-row>

      <el-col :span="12">

        <el-form-item label="用途">
          <el-input v-model="dataForm.userFor" placeholder="参数名" clearable  style="width: 500px"></el-input>
        </el-form-item>
      </el-col>

      <el-form-item>
        <el-button type="primary" @click="getDataList()" size="mini"> 查询 </el-button>
        <el-button @click="clearForm()" size="mini">清空</el-button>
        <el-button @click="downloadExcel()" size="mini">导出本页</el-button>
        <el-button @click="getDataList()" size="mini">导出全部</el-button>
        <!--          <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>-->
        <!--          <el-button type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>-->
      </el-form-item>

    </el-form>
    <hr style="opacity: 0.3"><br>

    <el-table
      size="mini"
      :data="dataList"
      border
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <!--      v-loading="dataListLoading"-->
      <el-table-column
        prop="code"
        header-align="center"
        align="center"
        label="工夹具代码">
      </el-table-column>
      <el-table-column
        prop="name"
        header-align="center"
        align="center"
        label="工夹具名称">
      </el-table-column>
      <el-table-column
        prop="workcell"
        header-align="center"
        align="center"
        label="所属部门">
      </el-table-column>
      <el-table-column
        prop="family"
        header-align="center"
        align="center"
        label="类别">
      </el-table-column>
      <el-table-column
        prop="stockCount"
        header-align="center"
        align="center"
        label="库存数量">
      </el-table-column>
      <el-table-column
        prop="userFor"
        header-align="center"
        align="center"
        label="用途">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <!--          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>-->
          <!--          <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>-->
          <el-button @click="addOrUpdateHandle(scope.row.id)" size="mini">查看明细</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from './jig-detail-add-or-update.vue'

export default {
  data() {
    return {
      dataForm: {
        name: '',
        name1: '',
        workcell: '',
        family: '',
        userFor: ''
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false
    }
  },
  components: {
    AddOrUpdate
  },
  activated() {
    this.getDataList()
  },
  methods: {
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      this.$http({
        url: `http://localhost:10000/gongjiajumain/jigdefinition/list?page=${this.pageIndex}&limit=${this.pageSize}&dataForm=${encodeURIComponent(JSON.stringify(this.dataForm))}`,
        method: 'get'
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list
          this.totalPage = data.page.totalCount
          console.log("555")
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    //dataForm=${encodeURIComponent(JSON.stringify(this.dataForm))}
    // getDataInfo() {
    //   this.dataListLoading = true
    //   this.$http({
    //     url: `http://localhost:10000/gongjiajumain/jigdefinition/info?page=${this.pageIndex}&limit=${this.pageSize}&dataForm=${encodeURIComponent(JSON.stringify(this.dataForm))}`,
    //     method: 'get'
    //   }).then(({data}) => {
    //     if (data && data.code === 0) {
    //       this.dataList = data.page.list
    //       this.totalPage = data.page.totalCount
    //     } else {
    //       this.dataList = []
    //       this.totalPage = 0
    //     }
    //     this.dataListLoading = false
    //   })
    // },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      console.log("555"+id)
      console.log("555")

      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
        console.log("444"+id)
      })
    },
    //清空
    clearForm() {
      this.dataForm.name = '';
      this.dataForm.name1 = '';
      this.dataForm.workcell = '';
      this.dataForm.family = '';
      this.dataForm.userFor = '';
    },
    downloadExcel() {
      console.log("11111111")
      this.spinShow = true;
      this.$http.post("http://localhost:10000/gongjiajumain/jigdefinition/exportExcel", this.dataList, {responseType: 'blob'})
        .then(res => {
          this.spinShow = false;
          const content = res.data;
          const fileName = '工夹具信息.xlsx';
          const blob = new Blob([content], { type: 'application/vnd.ms-excel' });
          const link = document.createElement('a');
          link.download = fileName;
          link.href = URL.createObjectURL(blob);
          link.click();
        })
        .catch(() => {
          this.spinShow = false;
          this.$message.error('导出Excel失败');
        });
    },
  }
}
</script>
